//
//  MessageViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/3/23.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MessageModel.h"
#import "MapDetailViewController.h"
#import "NotificationTableViewCell.h"
#import "NotificaView.h"
#import "IMMessageListViewController.h"
#import <JSBadgeView/JSBadgeView.h>
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic , strong) NotificaView * topView;
@end

@implementation MessageViewController
-(void)SQ_isShow{
    [self.topView SQ_show];
}
- (void)SQ_viewWillAppear:(BOOL)animated{
    [self.topView SQ_show];
   
}
 
- (void)viewDidLoad {
    [super viewDidLoad];
    self.zx_navTitle = SQNSLocalString(@"通知消息");
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_isShow) name:@"MessageViewController" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(SQ_number) name:@"IMMessageModel" object:nil];
   
}
- (void)SQ_UIViewForSelfView{
    UIImageView * bg = [[UIImageView alloc]init];
    bg.image = SQGetImage(@"listbg");
    bg.contentMode = UIViewContentModeScaleAspectFill;
    [self.view addSubview:bg];
    [bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    [self.view addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.equalTo(self.zx_navBar.mas_bottom);
        make.height.mas_equalTo(0);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = UIColor.clearColor;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.topView.mas_bottom);
            make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-50);
    }];
    [self SQ_tableViewAddRefresh];
    [self SQ_Header];
}
- (void)SQ_Header{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:@"1" forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url24 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page = 1;
        [self.tableView.mj_header endRefreshing];
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MessageModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray removeAllObjects];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_header endRefreshing];
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (void)SQ_Foother{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:[NSString stringWithFormat:@"%d",self.page+1] forKey:@"current"];
    [dic setObject:@"20" forKey:@"size"];
    [HttpTool post:Url24 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.page ++;
        NSDictionary * data = [[NSDictionary alloc]initWithDictionary:httpModel.data];
        NSArray * array = [MessageModel mj_objectArrayWithKeyValuesArray:[data objectForKey:@"records"]];
        [self.jsonArray addObjectsFromArray:array];
        [self.tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
     return self.jsonArray.count;
}
 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCell"];
    if (!cell) {
        cell = [[MessageTableViewCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:@"MessageTableViewCell"];
    }
    cell.model = [self.jsonArray objectAtIndex:indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return   [MessageTableViewCell FSCellHeightForTableView:tableView indexPath:indexPath cellContentViewWidth:0 bottomOffset:10];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    if (model.type == 1 || model.type == 2) {
        PointModel * mo = [PointModel new];
        mo.userid = model.rid;
        MapDetailViewController * vc = [[MapDetailViewController alloc]init];
        vc.model = mo;
        [self SQ_pushViewControllerFull:vc];
    }  
}

- (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf);
    UIContextualAction * delete = [UIContextualAction contextualActionWithStyle:UIContextualActionStyleDestructive title:SQNSLocalString(@"删除") handler:^(UIContextualAction * _Nonnull action, __kindof UIView * _Nonnull sourceView, void (^ _Nonnull completionHandler)(BOOL)) {
        completionHandler(YES);
        [weakSelf SQ_deleted:indexPath];
    }];
    delete.backgroundColor = UIColor.redColor;
    UISwipeActionsConfiguration * config = [UISwipeActionsConfiguration configurationWithActions:@[delete]];
    return config;
}
-(void)SQ_deleted:(NSIndexPath*)indexPath{
    MessageModel * model = [self.jsonArray objectAtIndex:indexPath.item];
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:model.userId forKey:@"id"];
    [HttpTool post:Url25 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [self.jsonArray removeObject:model];
        [self.tableView reloadData];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
- (NotificaView *)topView{
    if (!_topView) {
        _topView = [[NotificaView alloc]initWithFrame:CGRectZero];
    }
    return _topView;
}

 

@end
